<?php

class Express {

    public $gatewayUrl = "http://jisukdcx.market.alicloudapi.com/express/query";
    public $connectTimeout;
    public $appKey;
    public $secretKey;
    public $appCode;

    public function __construct($appKey = "", $secretKey = "", $appCode = "f788e40459484d7da97fa41eb59b2555") {
        $this->appKey = $appkey;
        $this->secretKey = $secretKey;
        $this->appCode = $appCode;
    }

    public function getInfo($no) {
        $headers = array(
            'Authorization' => "APPCODE ". $this->appCode,
            'Content-Type' => "application/json; charset=UTF-8"
        );
        $result = $this->curl($this->gatewayUrl.'?number='. $no .'&type=auto', 'GET', null, $headers);
        $result = json_decode($result, TRUE);
        if($result['status'] == 0){
            $value = $result['result'];
//            $re_arr = array();
//            if($value['list']) foreach($value['list'] as $k => $v){
//                $re_arr[$k] = $value['list'][COUNT($value['list']) - 1 - $k];
//            }
//            $value['list'] = $re_arr;
            return array('success' => true, 'msg' => '请求成功', 'data' => $value);
        }else{
            return array('success' => false, 'msg' => '请求失败'.$result['msg']);
        }
        
    }

    public function curl($url, $httpMethod = "GET", $postFields = null, $headers = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        //https request
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_array($headers) && 0 < count($headers)) {
            //curl_setopt($ch, CURLOPT_HEADER, true);
            $httpHeaders = self::getHttpHearders($headers);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        if($httpMethod == "POST"){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            //throw new Exception(curl_error($ch), 0);
            return false;
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                //throw new Exception($response, $httpStatusCode);
                return false;
            }
        }
        curl_close($ch);
        return $response;
    }

    static function getHttpHearders($headers) {
        $httpHeader = array();
        foreach ($headers as $key => $value) {
            array_push($httpHeader, $key . ":" . $value);
        }
        return $httpHeader;
    }

}